/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-7-14 下午3:34
 * Copyright (c) 2017. All rights reserved.
 */

package com.example.dqhl.jdmetro.adapter.life;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.dqhl.jdmetro.R;
import com.example.dqhl.jdmetro.bean.life.LifeLearn;
import com.example.dqhl.jdmetro.config.Config;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dqhl on 17/7/14.
 */
public class LifeCommonAdapter extends RecyclerView.Adapter<LifeCommonAdapter.LifeCommonViewHolder> {
    private Context mContext;
    private List<LifeLearn.DataBean> mLifeList = new ArrayList<>();
    private LayoutInflater mLayoutInflater;
    private onItemClickListener onItemClickListener;

    public LifeCommonAdapter(Context context) {
        this.mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
    }

    public void refresh_info(List<LifeLearn.DataBean> lifeList) {
        this.mLifeList = lifeList;
        notifyDataSetChanged();
    }
    //点击事件和长按事件
    public interface onItemClickListener {
        void onItemClick(View view, int position);

    }
    public void setOnItemClickListener(onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }
    @Override
    public LifeCommonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LifeCommonViewHolder viewHolder = new LifeCommonViewHolder(mLayoutInflater.inflate(R.layout.layout_life_common,parent,false));
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final LifeCommonViewHolder holder, int position) {
        x.image().bind(holder.iv_lifePic, Config.pic_base_url+mLifeList.get(position).getShop_pic());
        holder.tv_title.setText(mLifeList.get(position).getShop_name());
        holder.tv_lifeSecondtitle.setText(mLifeList.get(position).getShop_text());
        holder.tv_lifeaddress.setText(mLifeList.get(position).getShop_city());
        holder.tv_lifeDistance.setText(mLifeList.get(position).getDistance());
        holder.tv_lifePageViews.setText(mLifeList.get(position).getShop_pageviews());
        if(onItemClickListener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = holder.getLayoutPosition();
                    onItemClickListener.onItemClick(holder.itemView,position);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return mLifeList.size();
    }

    class LifeCommonViewHolder extends RecyclerView.ViewHolder {

        LinearLayout ll_lifeItem;
        TextView tv_title;
        TextView tv_lifeSecondtitle;
        TextView tv_lifeaddress;
        ImageView iv_lifePic;
        TextView tv_lifeDistance;
        TextView tv_lifePageViews;
        public LifeCommonViewHolder(View itemView) {
            super(itemView);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            tv_lifeSecondtitle = (TextView) itemView.findViewById(R.id.tv_lifeSecondtitle);
            tv_lifeaddress = (TextView) itemView.findViewById(R.id.tv_lifeaddress);
            iv_lifePic = (ImageView) itemView.findViewById(R.id.iv_lifePic);
            tv_lifeDistance = (TextView) itemView.findViewById(R.id.tv_lifeDistance);
            tv_lifePageViews = (TextView) itemView.findViewById(R.id.tv_lifePageViews);
            (ll_lifeItem) = (LinearLayout) itemView.findViewById(R.id.ll_lifeItem);
        }
    }
}